/**
 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 * 
 * 项目名：	cards-web
 * 文件名：	WxComponentAppDomainServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * 2017年5月24日 - Debenson - 创建。
 */
package me.chanjar.weixin.component.api.app.impl;

import java.util.Map;

import com.gomore.jeecg.common.Assert;
import com.google.common.collect.Maps;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.JsonMapper;
import me.chanjar.weixin.component.api.WxCompServiceDelegate;
import me.chanjar.weixin.component.api.app.WxCompMaDomainService;
import me.chanjar.weixin.component.bean.author.WxAppDomains;

/**
 * 修改托管小程序的域名
 * 
 * @author Debenson
 * @since 0.1
 */
public class WxCompMaDomainServiceImpl implements WxCompMaDomainService {

  private final WxCompServiceDelegate delegate;

  public WxCompMaDomainServiceImpl(WxCompServiceDelegate delegate) {
    this.delegate = delegate;
  }

  @Override
  public void addDomain(WxAppDomains domains) throws WxErrorException {
    Assert.assertArgumentNotNull(domains, "domains");

    ModifyAppDomainRequest req = ModifyAppDomainRequest.buildAdd(domains);
    modifyDomain(req);
  }

  @Override
  public void setDomain(WxAppDomains domains) throws WxErrorException {
    Assert.assertArgumentNotNull(domains, "domains");

    ModifyAppDomainRequest req = ModifyAppDomainRequest.buildSet(domains);
    modifyDomain(req);
  }

  @Override
  public void deleteDomain(WxAppDomains domains) throws WxErrorException {
    Assert.assertArgumentNotNull(domains, "domains");

    ModifyAppDomainRequest req = ModifyAppDomainRequest.buildDelete(domains);
    modifyDomain(req);
  }

  @Override
  public WxAppDomains getDomain() throws WxErrorException {
    String url = "https://api.weixin.qq.com/wxa/modify_domain";
    String req = ModifyAppDomainRequest.buildGet();
    String resp = delegate.httpPost(url, req);
    return JsonMapper.defaultMapper().fromJson(resp, WxAppDomains.class);
  }

  private void modifyDomain(ModifyAppDomainRequest req) throws WxErrorException {
    String url = "https://api.weixin.qq.com/wxa/modify_domain";
    delegate.httpPost(url, JsonMapper.defaultMapper().toJson(req));
  }

  /**
   * 修改域名请求
   * 
   * @author Debenson
   * @since 0.1
   */
  static class ModifyAppDomainRequest extends WxAppDomains {
    private static final long serialVersionUID = 3187834920006855181L;

    private static final String ACTION_ADD = "add";
    private static final String ACTION_DELETE = "delete";
    private static final String ACTION_SET = "set";
    private static final String ACTION_GET = "get";

    /* add添加, delete删除, set覆盖, get获取。当参数是get时不需要填四个域名字段。 */
    private String action = ACTION_ADD;

    public static ModifyAppDomainRequest buildAdd(WxAppDomains domains) {
      ModifyAppDomainRequest req = new ModifyAppDomainRequest(domains);
      req.setAction(ACTION_ADD);
      return req;
    }

    public static ModifyAppDomainRequest buildSet(WxAppDomains domains) {
      ModifyAppDomainRequest req = new ModifyAppDomainRequest(domains);
      req.setAction(ACTION_SET);
      return req;
    }

    public static ModifyAppDomainRequest buildDelete(WxAppDomains domains) {
      ModifyAppDomainRequest req = new ModifyAppDomainRequest(domains);
      req.setAction(ACTION_DELETE);
      return req;
    }

    public static String buildGet() {
      Map<String, Object> map = Maps.newConcurrentMap();
      map.put("action", ACTION_GET);
      return JsonMapper.defaultMapper().toJson(map);
    }

    public ModifyAppDomainRequest() {
    }

    public ModifyAppDomainRequest(WxAppDomains domains) {
      setRequestDomains(domains.getRequestDomains());
      setDownloadDomains(domains.getDownloadDomains());
      setUploadDomains(domains.getUploadDomains());
      setWsrequestDomains(domains.getWsrequestDomains());
    }

    public String getAction() {
      return action;
    }

    public void setAction(String action) {
      this.action = action;
    }

  }

}
